//
//  FRPGalleryViewControllerCollectionViewController.m
//  FunctionalReactivePixels
//
//  Created by Linyongzhi on 2017/3/31.
//
//

#import "FRPGalleryViewControllerCollectionViewController.h"
#import "FRPGalleryFlowLayout.h"
#import "FRPCell.h"

#import "FRPFullSizePhotoViewController.h"
#import "FRPFullSizePhotoViewModel.h"

#import "FRPGalleryViewModel.h"
#import "FRPPhotoImporter.h"

#import "RACDelegateProxy.h"

@interface FRPGalleryViewControllerCollectionViewController ()<FRPFullSizePhotoViewControllerDelegate>

//@property(nonatomic, strong) NSArray *photosArray;
@property(nonatomic, strong) FRPGalleryViewModel *viewModel;
@property(nonatomic, strong) id collectionViewDelegate;

@end

@implementation FRPGalleryViewControllerCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init {
    FRPGalleryFlowLayout *flowLayout = [[FRPGalleryFlowLayout alloc] init];
    self = [super initWithCollectionViewLayout:flowLayout];
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = NO;
    
    self.title = @"Popular on 500px";
    
    // Register cell classes
    [self.collectionView registerNib:[UINib nibWithNibName:@"FRPCell" bundle:nil] forCellWithReuseIdentifier:reuseIdentifier];
    
    self.viewModel = [[FRPGalleryViewModel alloc] init];
    
    // Do any additional setup after loading the view.
    @weakify(self);
    [RACObserve(self, viewModel.model) subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        [self.collectionView reloadData];
    }];
    
    RACDelegateProxy* viewControllerDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)];
    
    [[viewControllerDelegate rac_signalForSelector:@selector(userDidScroll:toPhotoAtIndex:) fromProtocol:@protocol(FRPFullSizePhotoViewControllerDelegate)] subscribeNext:^(RACTuple *value) {
        @strongify(self);
        [self.collectionView scrollToItemAtIndexPath:
         [NSIndexPath indexPathForItem:[value.second integerValue] inSection:0]
                                    atScrollPosition:UICollectionViewScrollPositionCenteredVertically animated:NO];
    }];
    
    self.collectionViewDelegate = [[RACDelegateProxy alloc] initWithProtocol:@protocol(UICollectionViewDelegate)];
    [[self.collectionViewDelegate rac_signalForSelector:@selector(collectionView:didSelectItemAtIndexPath:)]
     subscribeNext:^(RACTuple *arguments) {
         @strongify(self);
         NSIndexPath *indexPath = arguments.second;
         FRPFullSizePhotoViewModel *viewModel = [[FRPFullSizePhotoViewModel alloc] initWithPhotoArray:self.viewModel.model initialPhotoIndex:indexPath.item];
         
         FRPFullSizePhotoViewController *viewController = [[FRPFullSizePhotoViewController alloc]
                                                           init];
         viewController.viewModel = viewModel;
         viewController.delegate = (id<FRPFullSizePhotoViewControllerDelegate>)viewControllerDelegate;
         [self.navigationController pushViewController:viewController animated:YES];
     }];
    
    self.collectionView.delegate = self.collectionViewDelegate;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.viewModel.model.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FRPCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // Configure the cell
    [cell setPhotoModel:self.viewModel.model[indexPath.row]];
    
    return cell;
}

@end
